package MainPackage;
import java.awt.geom.Point2D;
import java.util.ArrayList;

class Graveyard
{
	public int[][] nodes;
	public ArrayList<Point2D> graveStones;
	public ArrayList<HauntedHole> hauntedHoles;
	public ArrayList<Edge> edges;
	public Point2D origin;
	public Point2D destination;
	
	public Graveyard(int w, int h, ArrayList<Point2D> graveStones, ArrayList<HauntedHole> hauntedHoles)
	{
		nodes = new int[w][h];
		this.graveStones = graveStones;
		this.hauntedHoles = hauntedHoles;
		this.edges = new ArrayList<Edge>();
		this.origin = new Point2D.Double(0,0);
		this.destination = new Point2D.Double(w - 1,h - 1);
		initialize();
	}
	
	private void initialize()
	{
		// Iterate over width.
		for (int i = 0; i < nodes.length; i++) 
		{	
			// Iterate over height.
			for (int j = 0; j < nodes[i].length; j++) 
			{
				if (this.graveStones.contains(new Point2D.Double(i,j)))
					continue;
				if (this.hauntedHoles.contains(new HauntedHole(new Point2D.Double(i,j),null, 0)))
				{
					HauntedHole currentHole = this.hauntedHoles.get(this.hauntedHoles
							.indexOf(new HauntedHole(new Point2D.Double(i,j),null, 0)));
					
					this.edges.add(new Edge(currentHole.origin, currentHole.destination, currentHole.time));
				}
				else 
				{
					// Look in all directions
					if (i - 1 >= 0 && !this.graveStones.contains(new Point2D.Double(i - 1,j))) // Left
						this.edges.add(new Edge(new Point2D.Double(i,j),new Point2D.Double(i - 1,j), 1));
					if (i + 1 < nodes.length && !this.graveStones.contains(new Point2D.Double(i + 1,j))) // Right
						this.edges.add(new Edge(new Point2D.Double(i,j),new Point2D.Double(i + 1,j), 1));
					if (j - 1 >= 0 && !this.graveStones.contains(new Point2D.Double(i,j - 1))) // Up
						this.edges.add(new Edge(new Point2D.Double(i,j),new Point2D.Double(i,j - 1), 1));
					if (j + 1 < nodes[i].length && !this.graveStones.contains(new Point2D.Double(i,j + 1))) // Down
						this.edges.add(new Edge(new Point2D.Double(i,j),new Point2D.Double(i,j + 1), 1));
				}
			}
		}
	}
}